<template>
  <!-- 还有其他container，所以添加一个class属性，给它设置样式 -->
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png"/>
        <span>我的云盘后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleMenu">|||</div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" unique-opened
                 :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <el-sub-menu v-for="menu in level1Menu" :key="menu.menuId" :index="menu.menuId + ''">
            <template #title>
              <el-icon>
                <user/>
              </el-icon>
              <span>{{ menu.menuName }}</span>
            </template>

            <el-menu-item v-for="subMenu in level2Menu(menu.menuId)" :key="subMenu.menuId" :index="subMenu.menuUrl"
                          @click="saveActiveMenu(subMenu.menuUrl)">
              <i class="el-icon-menu"></i>
              <span>{{ subMenu.menuName }}</span>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup name='Home'>
/* 获取当前应用的路由和路由器实例*/
import {useRouter} from 'vue-router'

const router = useRouter()

/* 引入ref、computed*/
import {ref, computed, inject, onMounted} from 'vue'

/*声明响应式数据*/
const menuList = ref([]);
const isCollapse = ref(false);
const activePath = ref('');

/*组件逻辑*/
// 计算属性：过滤一级菜单
const level1Menu = computed(() => {
  let list = menuList.value.filter((menu) => menu.parentMenuId === 0)
  return list;
});
// 计算属性：过滤二级菜单
const level2Menu = computed(() => {
  return function (parentMenuId) {
    return menuList.value.filter(
        (menu) => menu.parentMenuId === parentMenuId
    )
  }
});

// 注销：清空sessionStorage，然后跳转到登录页
const logout = function () {
  sessionStorage.clear()
  router.push('/login')
}

const getMenuList = async function () {
  menuList.value = [{
    menuId: 1,
    menuName: '用户管理',
    parentMenuId: 0
  }, {
    menuId: 2,
    menuName: '用户管理',
    parentMenuId: 1,
    menuUrl: '/home/user'
  }, {
    menuId: 4,
    menuName: '文件管理',
    parentMenuId: 0,
  }, {
    menuId: 5,
    menuName: '我的文件',
    parentMenuId: 4,
    menuUrl: '/home/file'
  }, {
    menuId: 6,
    menuName: '我的分享',
    parentMenuId: 4,
    menuUrl: '/home/share'
  }, {
    menuId: 7,
    parentMenuId: 4,
    menuName: '我的回收站',
    menuUrl: '/home/bin'
  }]
};
const toggleMenu = function () {
  isCollapse.value = !isCollapse.value;
};

const saveActiveMenu = function (activePathParam) {
  sessionStorage.setItem('activePath', activePathParam)
  activePath.value = activePathParam
}

onMounted(() => {
  getMenuList();
  activePath.value = sessionStorage.getItem('activePath') || '';
});

</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;

  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;

  div {
    display: flex;
    align-items: center;

    img {
      height: 40px;
    }

    span {
      margin-left: 20px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
